const KoaRouter = require('koa-router')

// 中间件/控制器
const {
  auth
} = require('../middleware/auth.middleware')
const {
  validator
} = require('../middleware/addr.middleware')
const { create,findAll,update,remove,setDefault } = require('../controller/addr.controller')


const router = new KoaRouter({
  prefix: '/address'
})



// 添加接口
router.post('/', auth, validator({
  consignee: "string",
  phone: {
    type: "string",
    formatter: /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/
  },
  address: "string",
}), create)



// 列表接口
router.get('/',auth,findAll)


// 修改接口
router.put('/:id',auth,validator({
  consignee: "string",
  phone: {
    type: "string",
    formatter: /^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/
  },
  address: "string",
}),update)

// 删除接口
router.delete('/:id',auth,remove)

// 设置默认地址
router.patch('/:id',auth,setDefault)

module.exports = router